var Mycontract = require("./tpl/Mycontract.xtpl");
var MyctDtl = require("./tpl/getctdetail.xtpl");
var OperModal = require("./tpl/orperatemodal.xtpl");
var Pagination = require("COMMON/modules/pagination-x/v1.0/");

require("./index.scss");
var cData = require("COMMON/js/config.province.city.data");
var Contract = ZFT.Util.Class({
    container: $("body"),
    EVENTS: {
        "click #schCtBtn": "getMyContract",
        "click .cancel,.close": "closeModal",
        "click #tbodyMyCt .invCt": "onpenInvModal",
        "click #editFinanceInfo": "editFinanceInfo"
    },
    init: function () {
        var that = this;
        that.getMyContract(1);
        var pagination = this.Pagination = new Pagination({
            container: $(".paginationWrap"),
            count: 5,
            showTotal: true,
            jump: true,
            filter: true
        });
        pagination.on("page.switch", function (toPage, currentPage, totalPage) {
            pagination.render({ current: currentPage, toPage: toPage }),
                that.getMyContract(toPage);
        })
    },
    //获取我的合同列表
    getMyContract: function (toPage) {
        var that = this;
        var tbody = $("#tbodyMyCt");
        var pagin = $(".paginationWrap");
        var pageSize = $("#paginationWrap").attr("data-size");
        ZFT.Util.Ajax(
            '/r/lease_index',
            {
                type: "POST",
                dataType: "json",
                params: {
                    page: toPage,
                    page_size: pageSize ? pageSize : 10
                },
                loading: function () {
                    pagin.hide();
                    var loading = '<tr><td colspan="9">加载中,请稍后...</td></tr>'
                    tbody.html(loading);
                },
                success: function (res) {
                    var list = res.data.list;
                    render = ZFT.Util.ParseTemplate(Mycontract);

                    //获取省市信息
                    /*   var getArea = function (prov, cityId) {
                           var province = cData[prov];
                           var prov_name = province.name;
                           var citys = province.city;
                           var city_name = "";
                           for (var i = 0; i < citys.length; i++) {
                               var city = citys[i];
                               var city_id = city.id;
                               if (city_id == cityId) {
                                   city_name = city.name;
                                   break;
                               }
                           }
                           return prov_name + " " + city_name;
                       }
   
                       if (list) {
                           for (var i = 0, len = list.length; i < len; i++) {
                               var item = list[i];
                               var prov = item.province;
                               var city = item.city;
                               var area = getArea(prov, city);
                               item["city_area"] = area;
                           }
   
                       }*/
                    if (res.code == 200) {
                        var html;
                        var dtype = ZFT.Util.getDtype(),
                            pers = ZFT.Util.getPermission();
                        list["dtype"] = dtype;
                        list["unsetBtn"]=($.inArray("38",pers)!==-1) ? 1 : 0;
                        html = render({ myconcract: list });
                        tbody.html(html);
                        that.Pagination.render({ current: toPage, toPage: toPage, total: res.data.total });
                        that.getContractDetail();
                    } else if (res.code == 102) {
                        var html = '<tr><td colspan="9">账号已过期，请重新<a href="login">登陆</a><td></tr>'
                        tbody.html(html);
                    } else {
                        var html = '<tr><td colspan="9">' + res.msg + '</td></tr>'
                        tbody.html(html);
                    }
                }
            })
    },
    //查看合同详细
    getContractDetail: function () {
        var _this = this;
        $("#tbodyMyCt .getCt").on("click", function (e) {
            var getCtBtn = $(e.currentTarget);
            var ordernum = getCtBtn.attr("data-id");
            var getConctDtl = $("#getConctDtl");
            var dtype = ZFT.Util.getDtype();
            ZFT.Util.Ajax(
                '/r/Lease_getMycontractInfo',
                {
                    type: "POST",
                    dataType: "json",
                    params: {
                        ordernum: ordernum
                    },
                    success: function (res) {
                        var data = res.data;
                        //转换时间戳
                        /*if (data.list) {
                            //获取租赁时间
                            var getTime = function (unixtime) {
                                // var date = new Date(parseInt(time) * 1000).toLocaleString(time).replace(/:\d{1,2}$/, ' ');
                                // var date = new Date(parseInt(time) * 1000).toLocaleString(time);
                                var time = new Date(unixtime * 1000);
                                var year = time.getFullYear();
                                var month = time.getMonth() + 1;
                                var date = time.getDate();
                                var dateTime = year + "-" + month + "-" + date;
                                return dateTime;
                            }


                            var item = data.list;
                            //时间戳转换
                            var begin_time = item.begin_time;
                            var end_time = item.end_time;
                            var beginTime = getTime(begin_time);
                            var endTime = getTime(end_time);
                            item["beginTime"] = beginTime;
                            item["endTime"] = endTime;
                        }
*/
                        var render = ZFT.Util.ParseTemplate(MyctDtl);
                        if (res.code == 200) {
                            var html = render({ data: data, dtype: dtype });
                            getConctDtl.html(html);
                            _this.initDatePicker();
                        } else if (res.code == 102) {
                            return ZFT.Util.STip("fail", "未登录,请重新登录", 4000, function () {
                                window.location.href = "/index/index/login";
                            });
                        } else {
                            return ZFT.Util.STip("fail", res.msg, 4000);
                        }
                    }

                })
        })
    },
    //打开失效合同模态框
    onpenInvModal: function (e) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        var ordernum = tarBtn.attr("data-id");
        var render = ZFT.Util.ParseTemplate(OperModal);
        var html = render();
        $("#modal").html(html);
        $(".u-modal-btn_conf").on("click", function () {
            ZFT.Util.Ajax("/r/Lease_forbidLease", {
                type: "POST",
                dataType: "json",
                params: {
                    ordernum: ordernum,
                    status: 0
                },
                success: function (res) {
                    if (res.code == 200) {
                        $("#modal").html("");
                        _this.getMyContract(1);
                        ZFT.Util.STip("success", res.msg, 4000);
                    } else {
                        ZFT.Util.STip("fail", res.msg, 4000);
                    }
                }
            })
        })
    },
    initDatePicker: function () {
        $("#beginDate").datepicker({
            format: "yyyy-mm-dd",
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });
        $('#endDate').datepicker({
            format: "yyyy-mm-dd",
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });

    },
    //关闭模态框
    closeModal: function (e) {
        var tarBtn = $(e.currentTarget);
        tarBtn.parents(".m-modal_bg").fadeOut(200);
    },
    //修改财务联系人信息
    editFinanceInfo: function (e) {
        var tarBtn = $(e.currentTarget);
        var leaseNum = tarBtn.attr("data-num");
        var fiName = $("#fiName").val();
        var fiTel = $("#fiTel").val();
        var regTel = /^1\d{10}$/;
        if (!fiName) return ZFT.Util.STip("fail", "财务人姓名不为空！")
        if (!regTel.test(fiTel)) return ZFT.Util.STip("fail", "财务人电话格式错误！")
        ZFT.Util.Ajax("/r/Lease_editLease", {
            type: "POST",
            dataType: "json",
            params: {
                ordernum: leaseNum,
                finance_user: fiName,
                finance_mobile: fiTel
            },
            success: function (res) {
                if (res.code == 200) {
                    ZFT.Util.STip("success", res.msg);
                } else {
                    ZFT.Util.STip("fail", res.msg);
                }
            }
        })
    }

})

$(function () {
    new Contract();
})